package com.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.app.R;
import com.app.constant.Constants;
import com.app.constant.Defaultcontent;
import com.app.http.HttpDataLoader;
import com.app.http.HttpDataReqUrl;
import com.app.model.request.ReqObj;
import com.app.model.response.InviteRes;
import com.app.model.response.ResObj;
import com.app.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.ShareBoardConfig;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ActivityInvite extends ActivityBaseMenuText {


    @BindView(R.id.tvInvite)
    TextView tvInvite;

    private UMShareListener mShareListener;

    public ShareAction shareAction;

    private HttpDataLoader dataLoader;

    private String inviteUrl = Defaultcontent.inviteUrl;

    private String shareTitle = Defaultcontent.shareTitle;

    private String shareDesc = Defaultcontent.shareDesc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite);
        ButterKnife.bind(this);
        setTitle("分享有礼");
        dataLoader = new HttpDataLoader(this);

        /**
         * 友盟分享功能代码
         */
        mShareListener = new CustomShareListener(this);
        shareAction = new ShareAction(this)
                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ)
                .setCallback(mShareListener)
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        UMWeb web = new UMWeb(inviteUrl);
                        web.setTitle(shareTitle);
                        web.setDescription(shareDesc);
                        web.setThumb(new UMImage(ActivityInvite.this, R.mipmap.ic_launcher));
                        new ShareAction(ActivityInvite.this).withMedia(web)
                                .setPlatform(share_media)
                                .setCallback(mShareListener)
                                .share();
                    }
                });
        getInvitecode();
    }

    private void getInvitecode() {
        ReqObj req = new ReqObj();
        dataLoader.POST(HttpDataReqUrl.URL_GET_INVITE_CODE, req);
    }


    @Override
    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
        if (url.equals(HttpDataReqUrl.URL_GET_INVITE_CODE)) {
            if (result.getCode() == Constants.CODE_SUCCESS) {
                if (result.getData() instanceof InviteRes) {
                    InviteRes res = (InviteRes) (result.getData());
                    if (res.getInviteUrl() != null && !res.getInviteUrl().equals("")) {
                        inviteUrl = res.getInviteUrl();
                    }
                    if (res.getShareTitle() != null && !res.getShareTitle().equals("")) {
                        shareTitle = res.getShareTitle();
                    }
                    if (res.getShareDesc() != null && !res.getShareDesc().equals("")) {
                        shareDesc = res.getShareDesc();
                    }
                }
            } else {
                ToastUtil.getInstance().showToast(this, result.getMsg());
            }
        }
    }

    @Override
    public void onRequestError(String url, int statusCode, String error) {
        ToastUtil.getInstance().showToast(this, error);
    }

    @OnClick(R.id.tvInvite)
    public void onViewClicked() {
        ShareBoardConfig config = new ShareBoardConfig();
        config.setShareboardPostion(ShareBoardConfig.SHAREBOARD_POSITION_CENTER);
        config.setMenuItemBackgroundShape(ShareBoardConfig.BG_SHAPE_CIRCULAR); // 圆角背景
//                config.setTitleVisibility(false); // 隐藏title
//                config.setCancelButtonVisibility(false); // 隐藏取消按钮
        shareAction.open(config);
        MobclickAgent.onEvent(this, "share");
    }

    private static class CustomShareListener implements UMShareListener {

        private WeakReference<com.app.MainActivity> mActivity;

        private CustomShareListener(ActivityInvite activity) {
            mActivity = new WeakReference(activity);
        }

        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {

            if (platform.name().equals("WEIXIN_FAVORITE")) {
                Toast.makeText(mActivity.get(), platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
            } else {
                if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                        && platform != SHARE_MEDIA.EMAIL
                        && platform != SHARE_MEDIA.FLICKR
                        && platform != SHARE_MEDIA.FOURSQUARE
                        && platform != SHARE_MEDIA.TUMBLR
                        && platform != SHARE_MEDIA.POCKET
                        && platform != SHARE_MEDIA.PINTEREST

                        && platform != SHARE_MEDIA.INSTAGRAM
                        && platform != SHARE_MEDIA.GOOGLEPLUS
                        && platform != SHARE_MEDIA.YNOTE
                        && platform != SHARE_MEDIA.EVERNOTE) {
                    Toast.makeText(mActivity.get(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
                }

            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastUtil.getInstance().showToast(mActivity.get(), platform + " 分享失败啦" + t.getMessage());
            if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                    && platform != SHARE_MEDIA.EMAIL
                    && platform != SHARE_MEDIA.FLICKR
                    && platform != SHARE_MEDIA.FOURSQUARE
                    && platform != SHARE_MEDIA.TUMBLR
                    && platform != SHARE_MEDIA.POCKET
                    && platform != SHARE_MEDIA.PINTEREST

                    && platform != SHARE_MEDIA.INSTAGRAM
                    && platform != SHARE_MEDIA.GOOGLEPLUS
                    && platform != SHARE_MEDIA.YNOTE
                    && platform != SHARE_MEDIA.EVERNOTE) {
                Toast.makeText(mActivity.get(), platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
                if (t != null) {
                    com.umeng.socialize.utils.Log.d("throw", "throw:" + t.getMessage());
                }
            }

        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastUtil.getInstance().showToast(mActivity.get(), platform + " 分享取消了");
//            Toast.makeText(mActivity.get(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
